#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Kang.Cunhua
# @Date:   2015-11-08 16:48:20
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-11-08 16:57:02
import threading
import time


class Th(threading.Thread):
    """docstring for Th"""

    def __init__(self, thread_name):
        threading.Thread.__init__(self)
        self.setName = (thread_name)

    def run(self):
        threadLock.acquire()
        # 获得锁之后再运行
        print 'this is thread ' + self.getName()
        for i in range(1, 10):
            time.sleep(0.5)
            print str(i)
        print self.getName() + ' is over'
        threadLock.release()
        # 释放锁
if __name__ == '__main__':
    threadLock = threading.Lock()
    # 设置全局锁
    thread1 = Th('thread1')
    thread2 = Th('thread2')
    thread1.start()
    thread2.start()
